package controller

import (
	"errors"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntityManager"
	"gitee.com/fierce_wolf/go-fox-edge-common/commSql"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Number"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/httpserver"
	"go-fox-edge-kernel-manager/utils/PageUtils"
	"strconv"
)

type periodRecordController struct {
}

func (e *periodRecordController) init() {
	httpserver.Server.POST("/period/record/object/page", e.selectEntityPage)
}

func (e *periodRecordController) selectEntityPage(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	taskName := Map.GetString(body, "taskName", "")
	recordBatch := Map.GetString(body, "recordBatch", "")
	deviceName := Map.GetString(body, "deviceName", "")
	objectName := Map.GetString(body, "objectName", "")
	pageNum := Map.GetInt(body, "pageNum", 1)
	pageSize := Map.GetInt(body, "pageSize", 10)

	// 简单校验参数
	if Map.HasNull(body, "pageNum", "pageSize") {
		return nil, errors.New("参数不能为空:pageNum, pageSize")
	}

	filter := ""
	if taskName != "" {
		entity := edgeEntity.Type.PeriodTaskEntity.Instance()
		entity.TaskName = taskName
		exist, err := commEntityManager.GetEntityByServiceKey(entity.Type(), entity.MakeServiceKey())
		if err != nil {
			return nil, err
		}
		if exist != nil {
			filter += " (task_id = " + strconv.Itoa(Number.MakeInt(exist.GetBaseEntity().Id)) + ") AND"
		}
	}
	if deviceName != "" {
		entity := edgeEntity.Type.DeviceEntity.Instance()
		entity.DeviceName = deviceName
		exist, err := commEntityManager.GetEntityByServiceKey(entity.Type(), entity.MakeServiceKey())
		if err != nil {
			return nil, err
		}
		if exist != nil {
			filter += " (device_id = " + strconv.Itoa(Number.MakeInt(exist.GetBaseEntity().Id)) + ") AND"
		}
	}
	if objectName != "" {
		filter += " (object_name = '" + objectName + "') AND"
	}
	if recordBatch != "" {
		filter += " (record_batch = '" + recordBatch + "') AND"
	}
	if filter != "" {
		filter = filter[0 : len(filter)-len("AND")]
	}

	result, err := e.selectEntityListPage(filter, "ASC", pageNum, pageSize)
	if result != nil {
		list := result["list"].([]map[string]interface{})
		e.extend(list)
	}

	return result, err
}

func (e *periodRecordController) extend(list []map[string]interface{}) {
	for _, data := range list {
		entity := commEntityManager.GetEntityById(edgeEntity.Type.DeviceEntity.Type(), Map.GetInt64(data, "deviceId", 0))
		if entity != nil {
			deviceEntity := entity.(*edgeEntity.DeviceEntity)

			data["deviceName"] = deviceEntity.DeviceName
			data["manufacturer"] = deviceEntity.Manufacturer
			data["deviceType"] = deviceEntity.DeviceType
			data["channelType"] = deviceEntity.ChannelType
			data["channelName"] = deviceEntity.ChannelName
		}

		entity = commEntityManager.GetEntityById(edgeEntity.Type.PeriodTaskEntity.Type(), Map.GetInt64(data, "taskId", 0))
		if entity != nil {
			periodTaskEntity := entity.(*edgeEntity.PeriodTaskEntity)

			data["taskName"] = periodTaskEntity.TaskName
		}
	}
}

func (e *periodRecordController) selectEntityListPage(filter string, order string, pageNum int, pageSize int) (map[string]interface{}, error) {
	// 从数据库的deviceObject中查询总数
	selectCount := PageUtils.MakeSelectCountSQL("tb_period_record", filter)

	total, err := commSql.QueryCount(selectCount)
	if err != nil {
		return nil, err
	}

	// 分页查询数据
	selectPage := PageUtils.MakeSelectSQLPage("tb_period_record", nil, nil, filter, order, pageNum, pageSize)
	entityList, err := commSql.Client.Query(edgeEntity.Type.PeriodRecordEntity.Type(), selectPage)
	if err != nil {
		return nil, err
	}

	mapList := commEntity.BuildMapList(entityList)

	data := make(map[string]interface{})
	data["list"] = mapList
	data["total"] = total

	return data, nil

}
